require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
require 'win32ole'
require 'win32/process'

class TC_ForumApprove_test < Test::Unit::TestCase
   
    def startup
      $GroupName= 'ForumGroup1'
      $GroupDescription = 'This is the first group for forums'
      $ForumName = 'Forum1'
      $ForumDescription = 'This is the first forum'
      $AppKey = 'ForumAppKey'
      $PostSubject = 'PostFirst'
      $PostDescription = 'Testing Anonymous posts and moderation'
      $DeleteReason ='Testing Delete Option'
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_0000
        require 'win32/process'
        $pid = Process.create(
        :app_name      => 'ruby ../resources/clickers.rb',
        :creation_flags  => Process::DETACHED_PROCESS
        ).process_id
  
    end  

    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        ie.button(:id, /loginButton$/).click
        test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
    def test_08_navigateToForumAdministration
        ie.goto($SITE_URL + 'controlpanel/forums/')
        assert(ie.contains_text('Forum Administration'))      
      end  
      
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Forum Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
      end

    def test_10_navigateToCreateForumGroup 
        ie.goto($SITE_URL +'controlpanel/forums/ForumGroups.aspx')
        assert(ie.button(:name,/AddButton$/).exists?)
        ie.button(:name,/AddButton$/).click
      end
      
    def test_11_navigateToCreateGroup
        assert(ie.text_field(:id,/ForumGroupName$/).exists?)
        ie.text_field(:id,/ForumGroupName$/).value =$GroupName
        assert(ie.text_field(:id,/ForumGroupDesc$/).exists?)
        ie.text_field(:id,/ForumGroupDesc$/).value =$GroupDescription
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
      end
      
    def test_13_navigteToCreateForum
        ie.goto($SITE_URL +'controlpanel/forums/SectionEdit.aspx')
        assert(ie.text_field(:id,/Name$/).exists?)
        ie.text_field(:id,/Name$/).value =$ForumName
        assert(ie.text_field(:id,/Description$/).exists?)
        ie.text_field(:id,/Description$/).value =$ForumDescription
        assert(ie.select_list(:id,/AdminGroupList$/).exists?)
        ie.select_list(:id,/AdminGroupList$/).select($GroupName)
        assert(ie.radio(:id,/IsActive_0$/).exists?)
        assert(ie.radio(:id,/IsActive_1$/).exists?)
        ie.radio(:id,/IsActive_0$/).set
        assert(ie.select_list(:id,/DefaultLanguage$/).exists?)
        ie.select_list(:id,/DefaultLanguage$/).select('Chinese (Traditional)')
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
     end   
    
    def test_14_EditForum
        ie.goto($SITE_URL +'controlpanel/forums/Forums.aspx')
        assert(ie.table(:id,'Listing').exists?)
        table1 = ie.table(:id,'Listing')
        rows = table1.row_count
        rowcount1 =1
        while rowcount1<=rows
            if (table1[rowcount1][1].link(:text,$ForumName).exists?)
               table1[rowcount1][5].button(:value ,'Edit').click
                break
             end   
         rowcount1=rowcount1+1
        end
        sleep 3
        #Basic
        assert(ie.radio(:id,/IsActive_0$/).exists?)
        assert(ie.radio(:id,/IsActive_1$/).exists?)
        ie.radio(:id,/IsActive_0$/).set
        #Advance
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SectionEdit1_TabStrip.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SectionEdit1_TabStrip.GetTabAtIndex(1))")
        assert(ie.radio(:id,/IsModerated_0$/).exists?)
        assert(ie.radio(:id,/IsModerated_1$/).exists?)
        ie.radio(:id,/IsModerated_0$/).set
        assert(ie.radio(:id,/EnableAnonymousPosting_0$/).exists?)
        assert(ie.radio(:id,/EnableAnonymousPosting_1$/).exists?)
        ie.radio(:id,/EnableAnonymousPosting_0$/).set
        sleep 2
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
      end
      
    def test_15_newPost
      test_02_verifyLoginLinksExist
      sleep 2
      ie.goto($SITE_URL+'forums/')
      sleep 5
      ie.link(:text, $GroupName).click
      sleep 3
      ie.link(:text, $ForumName).click
      sleep 2
      ie.link(:text,'Write a New Post').click
      sleep 3
      # Compose
      sleep 2
      assert(ie.text_field(:id,/PostForm_ctl05_PostSubject$/).exists?)
      ie.text_field(:id,/PostForm_ctl05_PostSubject$/).value =$PostSubject
      sleep 2
      ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_bcr_bcr_PostForm_ctl05_PostBody_ctl00_Editor').execCommand('mceSetContent', false,'"+$PostDescription+"')")
      assert(ie.button(:id,/PostForm_ctl05_PostButton$/).exists?)
      ie.button(:id,/PostForm_ctl05_PostButton$/).click
    end
    
    def test_16_moderatePost
      test_02_verifyLoginLinksExist
      test_03_navigateToLogin
      test_04_populateLoginForm
      test_05_submitLoginForm
      sleep 5
      ie.goto($SITE_URL+'controlpanel/default.aspx')
      sleep 2
      ie.goto($SITE_URL+'controlpanel/Moderation/')
      sleep 2
      ie.link(:href,/ModerateForum.aspx/).click
      sleep 2
      ie.link(:text,'Approve & View').click
      sleep 5
      ie.link(:text,'Delete').click
      sleep 4
      sleep 5
      ie.text_field(:id,/DeleteReason$/).value = $DeleteReason 
      sleep 5
      ie.button(:id,/DeletePost$/).click 
    end

    def test_17_DeleteForum
        ie.goto($SITE_URL+'controlpanel/forums/Forums.aspx')
        assert(ie.table(:id,'Listing').exists?)
        table1 = ie.table(:id, 'Listing')
        rowcount = table1.row_count
        rowcount1 =1
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$ForumName).exists?)
               table1[rowcount1][5].button(:id, /DeleteButton$/).click
               break
             end   
         rowcount1=rowcount1+1
        end
      end
      
    def test_18_DeleteForumGroup
      ie.goto($SITE_URL+'controlpanel/forums/ForumGroups.aspx')
      assert(ie.table(:id,'Listing').exists?)
      table1 = ie.table(:id, 'Listing')
      rowcount = table1.row_count
      rowcount1 =1
      while rowcount1<=rowcount
          if (table1[rowcount1][1].link(:text,$GroupName).exists?)
               table1[rowcount1][2].button(:value, 'Delete').click
               break
             end   
      rowcount1=rowcount1+1
      end
    end
    
    def test_99_Exit
        ie.close
    end
        
      
end